package com.longrise.android.byjk.advertisement.admanager;

import android.content.Context;
import android.text.TextUtils;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.app.BYJKAppLike;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by callxkj on 2016/12/20.
 * 用于初始化或者广告下载
 */

public class ADManagerHttpHelper {

    private static ADManagerHttpHelper mInstance;

    private ADManagerHttpHelper(){

    }

    public static ADManagerHttpHelper getInstance(){
        if(mInstance==null){
            mInstance = new ADManagerHttpHelper();
        }
        return mInstance;
    }


    public void initAdindex(){
        initAdindex(null);
    }

    /**
     * 获取广告图片
     */
    public void initAdindex(final ADIndexCallback callback) {
        Context context = BYJKAppLike.getAPP().getApplicationContext();
        String managerzoneno = ADCacheHelper.getInstance().getManagerzoneno();
        if (TextUtils.isEmpty(managerzoneno)) {
            callback.onFail();
            return;
        }
        EntityBean bean = new EntityBean();
        bean.set("managerzoneno", managerzoneno);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl,
                "bbt_getAppIndexAds", fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                parseResult(result,callback);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                ADCacheHelper.getInstance().saveADindexBean(null);
                if(callback!=null){
                    callback.onFail();
                }
            }

            @Override
            public void onFinished(String key, String service) {

            }
        });
    }



    /**
     * 解析结果并存储用于后面广告请求
     */
    private void parseResult(Object result,ADIndexCallback callback){
        try {
            if (result != null) {
                EntityBean bean = (EntityBean) result;
                int resultstate = (Integer) bean.get("resultstate");
                EntityBean resultbean = bean.getBean("result");
                if (resultstate == 1 && resultbean != null) {
                    String adssrc = resultbean.getString("adssrc");
                    String adshref = resultbean.getString("adshref");
                    long saveTime = System.currentTimeMillis();
                    ADIndexbean indexbean = new ADIndexbean();
                    indexbean.setAdssrc(adssrc);
                    indexbean.setAdshref(adshref);
                    indexbean.setSaveTime(saveTime);
                    ADCacheHelper.getInstance().saveADindexBean(indexbean);
                    if(callback!=null){
                        callback.onSuccess();
                    }
                }else{
                    ADCacheHelper.getInstance().saveADindexBean(null);
                    if(callback!=null){
                        callback.onFail();
                    }
                }
            }else{
                ADCacheHelper.getInstance().saveADindexBean(null);
                if(callback!=null){
                    callback.onFail();
                }
            }
        } catch (Exception e) {
            ADCacheHelper.getInstance().saveADindexBean(null);
        }
    }


}
